package com.ddz.implementation;

import java.util.ArrayList;
import java.util.List;

enum OBJECT_GROUPS {
	OBJECT_GROUP_PLAYER, OBJECT_GROUP_CARD, OBJECT_GROUP_NUMBER;
}

public class ObjectManager {

	private static ObjectManager mInstance = null;

	

	private final List<ArrayList<Entity>> mAllObjects; // A list containing all
														// objects

	private ObjectManager() {
		mAllObjects = new ArrayList<ArrayList<Entity>>(
				OBJECT_GROUPS.OBJECT_GROUP_NUMBER.ordinal()); // create
		// all
		// objects
		// in
		// freeObjects
	}

	public void cleanUp() {
		// First release all the players
		for (int iter = 0; iter < mAllObjects.get(
				OBJECT_GROUPS.OBJECT_GROUP_PLAYER.ordinal()).size(); iter++)
			mAllObjects.get(OBJECT_GROUPS.OBJECT_GROUP_PLAYER.ordinal())
					.get(iter).release();

		// Secondly release the all cards
		for (int iter = 0; iter < mAllObjects.get(
				OBJECT_GROUPS.OBJECT_GROUP_CARD.ordinal()).size(); iter++)
			mAllObjects.get(OBJECT_GROUPS.OBJECT_GROUP_CARD.ordinal())
					.get(iter).release();

		// Clear the rest of the objects
		mAllObjects.get(OBJECT_GROUPS.OBJECT_GROUP_PLAYER.ordinal()).clear();
		mAllObjects.get(OBJECT_GROUPS.OBJECT_GROUP_CARD.ordinal()).clear();
	}

	// Add an object to the Manager
	public void addObject(Entity object, OBJECT_GROUPS objectGroup) {
		// Add the object to the mAllObjects list
		// Send it to the appropriate group
		object.setObjectId(mAllObjects.get(objectGroup.ordinal()).size());
		mAllObjects.get(objectGroup.ordinal()).add(object);
	}

	public void removeObject(Entity object, OBJECT_GROUPS objectGroup) {
		if (object != null
				&& object.getObjectId() < mAllObjects.get(objectGroup.ordinal()).size())
			mAllObjects.get(objectGroup.ordinal()).remove(objectGroup);
	}

	// Update all of the objects in the Manager
	public void update() {
		// First update all players
		for (int iter = 0; iter < mAllObjects.get(
				OBJECT_GROUPS.OBJECT_GROUP_PLAYER.ordinal()).size(); iter++)
			mAllObjects.get(OBJECT_GROUPS.OBJECT_GROUP_PLAYER.ordinal())
					.get(iter).onUpdate(1.0f);

		// Secondly update all cards
		for (int iter = 0; iter < mAllObjects.get(
				OBJECT_GROUPS.OBJECT_GROUP_CARD.ordinal()).size(); iter++)
			mAllObjects.get(OBJECT_GROUPS.OBJECT_GROUP_CARD.ordinal())
					.get(iter).onUpdate(1.0f);
	}

	// Returns the instance of the ObjectManager
	public static ObjectManager getInstance() {
		if (mInstance == null)
			mInstance = new ObjectManager();
		return mInstance;
	}

	// Returns all of the players in list form
	public List<Entity> getPlayers() {
		return mAllObjects.get(OBJECT_GROUPS.OBJECT_GROUP_PLAYER.ordinal());
	}

	// Returns all of the cards in list form
	public List<Entity> getCards() {
		return mAllObjects.get(OBJECT_GROUPS.OBJECT_GROUP_CARD.ordinal());
	}
}
